﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace XDCCManager.Model.Actions
{
	public class ActionGroup : BaseModel
	{
		public ActionGroup()
		{
			Actions = new ObservableCollection<IAction>();
			Actions.CollectionChanged += Actions_CollectionChanged;
			OtherActions = new MyEnumerable<IAction>(from action in Actions where action != FirstAction select action);
		}

		void Actions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			int start = e.NewStartingIndex, end = e.OldStartingIndex;
			List<IAction> startList = (e.NewItems != null) ? (new List<IAction>(e.NewItems.OfType<IPackAction>())) : (null);
			List<IAction> endList = (e.OldItems != null) ? (new List<IAction>(e.OldItems.OfType<IPackAction>())) : (null);
			int count;
			switch (e.Action)
			{
				case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
				case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
					if (start == 0)
					{
						count = startList.Count;
						startList.Clear();
						for (int i = 0; (i < count) && ((i + 1) < Actions.Count); i++)
						{
							startList.Add(Actions[i + 1]);
						}
						FirstAction = Actions[0];
						OnPropertyChanged("FirstAction");
					}
					else
						start--;
					if (startList.Count > 0)
						(OtherActions as MyEnumerable<IAction>).OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(e.Action, startList, start));
					break;
				case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
				case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
				case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
					FirstAction = Actions.Count > 0 ? Actions[0] : null;
					OnPropertyChanged("FirstAction");
					(OtherActions as MyEnumerable<IAction>).OnCollectionChanged(e);
					break;
			}
		}

		public ObservableCollection<IAction> Actions { get; private set; }

		public IAction FirstAction { get; private set; }

		public IEnumerable<IAction> OtherActions { get; private set; }

		private class MyEnumerable<T> : IEnumerable<T>, System.Collections.Specialized.INotifyCollectionChanged
		{
			private IEnumerable<T> source;

			public MyEnumerable(IEnumerable<T> source)
			{
				this.source = source;
			}

			#region IEnumerable<T> Members

			public IEnumerator<T> GetEnumerator()
			{
				return source.GetEnumerator();
			}

			#endregion

			#region IEnumerable Members

			System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
			{
				return ((System.Collections.IEnumerable)source).GetEnumerator();
			}

			#endregion

			#region INotifyCollectionChanged Members

			public event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;

			#endregion

			internal void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
			{
				if (CollectionChanged != null)
					CollectionChanged(this, args);
			}
		}
	}
}
